package com.he.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.github.pagehelper.PageInfo;
import com.he.pojo.Department;
import com.he.service.department.DepartmentServiceI;
/**
 * 
 * @类名: DepartmentController
 * @描述： TODO(部门的增删改查管理)
 * @作者：陈世朝 
 * @日期： 2018年8月23日  下午10:23:15
 *
 */


//显示部门
@Controller
@RequestMapping("depart")
public class DepartmentController {
	@Autowired
	DepartmentServiceI dsi;
	@RequestMapping("/show")
	public ModelAndView fun(Department department) {
		List<Department> list=dsi.getDepartments(null, department);
		ModelAndView mv=new ModelAndView();
		mv.addObject("departlist", list);
		mv.setViewName("departmanage");
		/*为department总页数赋值,向前端传总页数*/
		PageInfo<Department> info=new PageInfo<>(list);
		//为department总页数赋值
		department.setPages(info.getPages());
		//为department总记录数赋值
		department.setTotal(info.getTotal());
		/*为department返回的controller链接赋值*/
		department.setUrl("show.do");
		/*向前端传参数保存在user中*/
		mv.addObject("department", department);
		return mv;
	}
	//删除部门
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, String>  fun1(int id){
		int r=dsi.delete(id);
		 Map<String,String> map = new HashMap<String, String>();
		if(r>0) {
			map.put("ret", "删除成功");
		}else {
			map.put("ret", "删除失败");
		}
		return map;
	}
	//跳到添加页面
	@RequestMapping("/showadddepart")
	public String fun2() {
		return "adddepart";
	}
	//添加部门
	@RequestMapping("/insert")
	public String fun4(Department department) {
		int r=dsi.insert(department);
		if(r>0) {
			return "redirect:/depart/show.do";
		}else {
			return "redirect:/depart/showaddepart.do";
		}
	}
     //跳到修改页面
	@RequestMapping("/showeditdepart")
	public ModelAndView fun3(int id) {
		Department department=dsi.getDepartmentById(id);
		ModelAndView mv=new ModelAndView();
		mv.addObject("list", department);
		
		mv.setViewName("editDepart");
		return mv;
	}
	//完成修改
	@RequestMapping("/update")
	public String fun5(Department department) {
		
		int r=dsi.update(department);
		if(r>0) {
			return "redirect:/depart/show.do";
		}else {
			 return "redirect:/depart/showeditdepart.do";
		}
	}
	
}
